﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Xml;

namespace ProjetoFase2
{
    public partial class FormLogin : Form
    {
        private UserLogin userLogin;

        public FormLogin()
        {
            // initialize the form
            InitializeComponent();
        } 
        private void FormLogin_Close()
        {
            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtServerIp.Text = "";
            txtServerPort.Text = "";
            txtServerIp.Focus();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            bool validLogin;
            btnEnter.Enabled = false;

            // loads the publication with the user login
            try
            {   // check if the login information is valid
                userLogin = validateLogin();

                if (userLogin == null)
                    validLogin = false;
                else
                    validLogin = true;
            }
            catch (Exception)
            {
                validLogin = false;
            }

            // if it is then load the info
            if (validLogin)
            {
                // load the new form to manage activities
                FormManageActivities form = 
                    new FormManageActivities(txtServerIp.Text,int.Parse(txtServerPort.Text), userLogin);
                // show the next form
                form.Show();

                // returns from a click to logout
                // drops the subscription
                // sets the text field to default values
                txtUsername.Text = "";
                txtPassword.Text = "";
            }
            btnEnter.Enabled = true;
        }

        private UserLogin validateLogin()
        {
            ServerInteraction server = new ServerInteraction(txtServerIp.Text, int.Parse(txtServerPort.Text));
            return server.executeLogin(txtUsername.Text, txtPassword.Text);
        }

        private void txtUsername_TextChanged(object sender, EventArgs e)
        {
            if (btnRestore.Enabled == false && txtUsername.Text != "")
            {
                btnRestore.Enabled = true;
            }
            else if (btnRestore.Enabled == true && txtUsername.Text == "")
            {
                btnRestore.Enabled = false;
            }
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            btnRestore.Enabled = false;

            ServerInteraction server = new ServerInteraction(txtServerIp.Text, 
                ((txtServerPort.Text == "") ? 0 : (int.Parse(txtServerPort.Text))));

            userLogin = new UserLogin();

            if (server.restoreDatabase(txtUsername.Text, txtPassword.Text, userLogin))
            {
                // load the new form to manage activities
                FormManageActivities form =
                    new FormManageActivities(txtServerIp.Text,
                        ((txtServerPort.Text == "") ? 0 : (int.Parse(txtServerPort.Text))), userLogin);
                // show the next form
                form.Show();

                // returns from a click to logout
                // drops the subscription
                // sets the text field to default values
                txtUsername.Text = "";
                txtPassword.Text = "";
            }

            btnRestore.Enabled = true;
        }

        private void label2_ParentChanged(object sender, EventArgs e)
        {

        }
    }
}